<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <script src="http://cdn.bootcss.com/angular.js/1.6.2/angular.js"></script>
    </head>
    <body ng-app="hp" ng-controller="ctrl">
        书名：{{goods.data.title}}<br/>
        价格：{{goods.data.price}}元<br/>
        库存：{{goods.data.stock}}本<br/>
        <br/>
        购买数量：{{goods.data.buyNum}}本<br/>
        总计：{{goods.data.buyNum*goods.data.price}}元<br/><br/>
        <button ng-click="goods.add()">增加购买数量</button>
        <button ng-click="goods.reduce()">减少购买数量</button>
    </body>
    <script>
    var ng = angular.module("hp",[]);
    ng.controller("ctrl",["$scope",function($scope){
        // $scope.goods = {"title":"CSS Magic","price":"320","stock":"6","buyNum":"0"};
        // $scope.add = function(){
        //     $scope.goods.buyNum = Math.min(++$scope.goods.buyNum,$scope.goods.stock);
        // }
        // $scope.reduce = function(){
        //     $scope.goods.buyNum = Math.max(--$scope.goods.buyNum,0);
        // }
        $scope.goods = {
            data : {"title":"CSS Magic","price":"320","stock":"6","buyNum":"0"},
            add : function(){
                $scope.goods.data.buyNum = Math.min(++$scope.goods.data.buyNum,$scope.goods.data.stock);
            },
            reduce : function(){
                $scope.goods.data.buyNum = Math.max(--$scope.goods.data.buyNum,0);
            }
        }
    }])
    </script>
</html>